package customer.zspt.cn.customer.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;

import java.util.List;

import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.Widget.chat.ChatArticleMsgItem;
import customer.zspt.cn.customer.Widget.chat.ChatFileRightMsgItem;
import customer.zspt.cn.customer.Widget.chat.ChatMsgImage;
import customer.zspt.cn.customer.Widget.chat.ChatVocieMsgItem;
import customer.zspt.cn.customer.bean.ChatMsgDao;
import customer.zspt.cn.customer.constant.chatMsgFieldName;
import customer.zspt.cn.customer.constant.constant;
import customer.zspt.cn.customer.utils.Enjoy;
import customer.zspt.cn.customer.utils.TimestampToBj;
import customer.zspt.cn.customer.utils.TvToEnjoy;

/**
 * Created by Administrator on 2018/8/24 0024.
 */

public class HistoryChatMsgAdapter extends BaseAdapter {
    List<ChatMsgDao> data;
    Context mContext;
    Enjoy enjoyCs;
    TvToEnjoy util;
    BitmapUtils utils;
    private long ChatTime = 0;

    public HistoryChatMsgAdapter(List<ChatMsgDao> data, Context context) {
        this.data = data;
        mContext = context;
        this.enjoyCs = new Enjoy(context);
        this.util = new TvToEnjoy(context);
        this.utils = new BitmapUtils(context, constant.IMAGEPATH);
    }
    @Override
    public int getViewTypeCount() {
        return 6;
    }
    @Override
    public int getItemViewType(int position) {
        if (data.get(position).getMsgType() == chatMsgFieldName.CONTENT) {
            return chatMsgFieldName.CONTENT;
        } else if (data.get(position).getMsgType() == chatMsgFieldName.IMAGE) {
            return chatMsgFieldName.IMAGE;
        } else if (data.get(position).getMsgType() == chatMsgFieldName.VOICE) {
            return chatMsgFieldName.VOICE;
        } else if (data.get(position).getMsgType() == chatMsgFieldName.ESSAY) {
            return chatMsgFieldName.ESSAY;
        } else if (data.get(position).getMsgType() == chatMsgFieldName.FILE) {
            return chatMsgFieldName.FILE;
        }
        return chatMsgFieldName.CONTENT;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        ChatMsgDao dao = data.get(position);
        String[] split = dao.getChatContent().split("\\[");
        int aboutMsg = data.get(position).getSendOrfrom();
        switch (type) {
            case chatMsgFieldName.CONTENT:
                if (aboutMsg == chatMsgFieldName.FROM) {
                    return addFromTextMsg(dao, convertView);
                } else {
                    return addMineTextMsg(dao, convertView);
                }
            case chatMsgFieldName.IMAGE:
                if (aboutMsg == chatMsgFieldName.FROM) {
                    return addMineImageMsg(dao, split, true, (ChatMsgImage) convertView);
                } else {
                    return addMineImageMsg(dao, split, false, (ChatMsgImage) convertView);
                }
            case chatMsgFieldName.FILE:
                if (aboutMsg == chatMsgFieldName.FROM) {
                    return addMineFileMsg(dao, split, true, (ChatFileRightMsgItem) convertView);
                } else {
                    return addMineFileMsg(dao, split, false, (ChatFileRightMsgItem) convertView);
                }
            case chatMsgFieldName.VOICE:
                if (aboutMsg == chatMsgFieldName.FROM) {
                    return addMineVocieMsg(dao, split, true, (ChatVocieMsgItem) convertView);
                } else {
                    return addMineVocieMsg(dao, split, false, (ChatVocieMsgItem) convertView);
                }
            case chatMsgFieldName.ESSAY:
                if (aboutMsg == chatMsgFieldName.FROM) {
                    return addMineArticleMsg(dao, split, true, (ChatArticleMsgItem) convertView);
                } else {
                    return addMineArticleMsg(dao, split, false, (ChatArticleMsgItem) convertView);
                }
        }
        return addMineTextMsg(dao, convertView);
    }

    private ChatMsgImage addMineImageMsg(ChatMsgDao dao,
                                         String[] split, boolean isLeft, ChatMsgImage chatMsgMineImage) {
        String IW;
        IW = split[1].replaceAll("\\]", "");
        if (chatMsgMineImage == null) {
            chatMsgMineImage = new ChatMsgImage(mContext, isLeft);
        }
        chatMsgMineImage.setHeader_portrait(dao.getAvater());
        chatMsgMineImage.setMsg_img(IW);
        chatMsgMineImage.setTime(dao.getTempTime(), ChatTime);
        ChatTime = dao.getTempTime();
        return chatMsgMineImage;
    }

    private ChatVocieMsgItem addMineVocieMsg(ChatMsgDao dao, String[] split, boolean isLeft, ChatVocieMsgItem rightMsgItem) {
        if (rightMsgItem == null) {
            rightMsgItem = new ChatVocieMsgItem(mContext, isLeft);
        }
        String IW;
        IW = split[1].replaceAll("\\]", "");
        String content[] = IW.split("\\|");
        rightMsgItem.audioTime(content[1]);
        rightMsgItem.setHttpAudio(content[0]);
        rightMsgItem.setTime(dao.getTempTime(), ChatTime);
        rightMsgItem.setAvater(dao.getAvater());
        ChatTime = dao.getTempTime();
        return rightMsgItem;
    }

    private ChatFileRightMsgItem addMineFileMsg(ChatMsgDao dao, String[] split,
                                                boolean isLeft, ChatFileRightMsgItem fileRightMsgItem) {
        if (fileRightMsgItem == null) {
            fileRightMsgItem = new ChatFileRightMsgItem(mContext, isLeft);
        }
        String IW;
        IW = split[1].replaceAll("\\]", "");
        String[] fileBasic = IW.split("\\|");
        fileRightMsgItem.setHeader_portrait(dao.getAvater());
        fileRightMsgItem.setTime(dao.getTempTime(), ChatTime);
        fileRightMsgItem.setfileUrl(fileBasic[0]);
        fileRightMsgItem.setFileTitle(fileBasic[1]);
        fileRightMsgItem.setFile_Size(fileBasic[2]);
        ChatTime = dao.getTempTime();
        return fileRightMsgItem;
    }
    private ChatArticleMsgItem addMineArticleMsg(ChatMsgDao dao, String[] split, boolean isLeft, ChatArticleMsgItem articleRightMsgItem) {
        String IW;
        IW = split[1].replaceAll("\\]", "");
        String[] essayBasic = IW.split("\\|");
        if (articleRightMsgItem == null) {
            articleRightMsgItem = new ChatArticleMsgItem(mContext, isLeft);
        }
        articleRightMsgItem.setArticleUrl(essayBasic[0]);
        articleRightMsgItem.setArticle_ig(essayBasic[1]);
        articleRightMsgItem.setArticleTitle(essayBasic[2]);
        articleRightMsgItem.setArticle_outline(essayBasic[3]);
        articleRightMsgItem.setHeader_portrait(dao.getAvater());
        articleRightMsgItem.setTime(dao.getTempTime(), ChatTime);
        ChatTime = dao.getTempTime();
        return articleRightMsgItem;
    }
    private View addFromTextMsg(ChatMsgDao dao, View view) {
        if (view == null) {
            view = View.inflate(mContext, R.layout.chat_msg_ts_right_item, null);
        }
        //消息内容
        TextView Tv_msgContent = (TextView) view.findViewById(R.id.Tv_msgContent);
        TextView msg_time = (TextView) view.findViewById(R.id.msg_time);
        CharSequence charSequence = solveData(dao.getChatContent());
        CharSequence replace = util.replace(charSequence);
        Tv_msgContent.setText(replace);
        //消息内容
        ImageView userIg = (ImageView) view.findViewById(R.id.header_portrait);
        if (dao.getTempTime() - ChatTime > 3 * 60 * 1000) {
            msg_time.setText(TimestampToBj.getBeijingTime(dao.getTempTime()));
        } else {
            msg_time.setVisibility(View.GONE);
        }
        ChatTime = dao.getTempTime();
        utils.display(userIg, dao.getAvater());
        return view;
    }

    private View addMineTextMsg(ChatMsgDao dao, View convertView) {
        //如果是我发送的
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.chat_msg_ts_left_item, null);
        }
        //消息内容
        TextView Tv_msgContent = (TextView) convertView.findViewById(R.id.Tv_msgContent);
        TextView msg_time = (TextView) convertView.findViewById(R.id.msg_time);
        CharSequence charSequence = solveData(dao.getChatContent());
        CharSequence replace = util.replace(charSequence);
        Tv_msgContent.setText(replace);
        ImageView userIg = (ImageView) convertView.findViewById(R.id.header_portrait);
        if (dao.getTempTime() - ChatTime > 3 * 60 * 1000) {
            msg_time.setText(TimestampToBj.getBeijingTime(dao.getTempTime()));
        } else {
            msg_time.setVisibility(View.GONE);
        }
        utils.display(userIg, dao.getAvater());
        ChatTime = dao.getTempTime();
        return convertView;
    }
    private CharSequence solveData(String msg) {
        for (int i = 0; i < enjoyCs.mEnjoyList.length; i++) {
            msg = msg.replace("face" + enjoyCs.mEnjoyList[i], enjoyCs.mEnjoyList[i]);
        }
        return util.replace(msg);
    }

    class ImageViewHolder{

    }

    class VoiceViewHolder{

    }
    class TextViewHolder{

    }
    class ArticleViewHolder{

    }
    class FileViewHolder{

    }
    class OrderViewHolder{

    }
}
